package com.example.hotel;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Service extends Activity implements OnClickListener {
	ArrayList<String> name_service = new ArrayList<String>();
	ArrayList<String> image_service = new ArrayList<String>();
	int indext = 3;
	int i = 0;
	int i0 = 0;
	int i1 = 1;
	int i2 = 2;
	Integer item = 0;
	Integer item1 = 0;
	Bitmap[] data = null;
	Button bt_left, bt_right;
	TextView tv_txt1, tv_txt2, tv_txt3, tv_menu, wellcome;
	ImageView imv_img1, imv_img2, imv_img3, map, home,image_cart,image_msg,image_bill;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hotel_info);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		TextView tvAmount = (TextView)findViewById(R.id.tvAmount);
		tvAmount.setText("("+((Global)getApplicationContext()).cart_count.toString()+")");
		getService_cat();
		bt_left = (Button) findViewById(R.id.left);
		bt_right = (Button) findViewById(R.id.right);
		imv_img1 = (ImageView) findViewById(R.id.image1);
		imv_img2 = (ImageView) findViewById(R.id.image2);
		imv_img3 = (ImageView) findViewById(R.id.image3);
		home = (ImageView) findViewById(R.id.imagehome);
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		tv_txt1 = (TextView) findViewById(R.id.tv1);
		tv_txt2 = (TextView) findViewById(R.id.tv2);
		tv_txt3 = (TextView) findViewById(R.id.tv3);
		tv_menu = (TextView) findViewById(R.id.tvMenu);
		bt_left.setOnClickListener(this);
		bt_right.setOnClickListener(this);
		imv_img1.setOnClickListener(this);
		imv_img2.setOnClickListener(this);
		imv_img3.setOnClickListener(this);
		home.setOnClickListener(this);
		image_cart.setOnClickListener(this);
		image_msg.setOnClickListener(this);
		image_bill.setOnClickListener(this);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		wellcome.setText(((Global) getApplicationContext()).wellcome);
		((Global)getApplicationContext()).callpage1 = com.example.hotel.Service.class;
		data = new Bitmap[image_service.size()];
		for (int i = 0; i < image_service.size(); i++) {
			String tmp = String.valueOf(image_service.get(i));
			
			Bitmap temp = getBitmapFromURL(tmp);
			getBitmapFromURL(null);
			if (temp != null) {
				data[i] = temp;
			}
		}
		imv_img1.setImageBitmap(data[i0]);
		imv_img2.setImageBitmap(data[i1]);
		imv_img3.setImageBitmap(data[i2]);
		String str = ((Global) getApplicationContext()).nameMenu;
		tv_menu.setText(str);
		tv_txt1.setText(name_service.get(i0));
		tv_txt2.setText(name_service.get(i1));
		tv_txt3.setText(name_service.get(i2));
		item = data.length % 3;
		item1 = data.length - item;

		/*------setfonts------*/
		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		tv_txt1.setTypeface(Font);
		tv_txt2.setTypeface(Font);
		tv_txt3.setTypeface(Font);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		tv_menu.setTypeface(FontBold);
		/*--------------------*/
		((Global)getApplicationContext()).page_service = false;
		((Global) getApplicationContext()).callpage1 = com.example.hotel.Service.class;

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.right:
			if (item == 1 && i + 3 == item1) {

				i += 3;
				i0 += 3;
				i1 += 3;
				i2 += 3;
				imv_img1.setImageBitmap(data[i0]);
				imv_img2.setImageBitmap(null);
				imv_img2.setEnabled(false);
				imv_img3.setImageBitmap(null);
				imv_img3.setEnabled(false);
				tv_txt1.setText(name_service.get(i0));
				tv_txt2.setText(" ");
				tv_txt3.setText(" ");

			} else if (item == 2 && i + 3 == item1) {
				i += 3;
				i0 += 3;
				i1 += 3;
				i2 += 3;
				imv_img1.setImageBitmap(data[i0]);
				imv_img2.setImageBitmap(data[i1]);
				imv_img3.setImageBitmap(null);
				imv_img3.setEnabled(false);
				tv_txt1.setText(name_service.get(i0));
				tv_txt2.setText(name_service.get(i1));
				tv_txt3.setText("");

			} else if (i2 + 3 < data.length) {
				i += 3;
				i0 += 3;
				i1 += 3;
				i2 += 3;
				imv_img1.setImageBitmap(data[i0]);
				imv_img2.setImageBitmap(data[i1]);
				imv_img3.setImageBitmap(data[i2]);
				imv_img1.setEnabled(true);
				imv_img2.setEnabled(true);
				imv_img3.setEnabled(true);
				tv_txt1.setText(name_service.get(i0));
				tv_txt2.setText(name_service.get(i1));
				tv_txt3.setText(name_service.get(i2));
			}

			break;
		case R.id.left:
			if (i > 0 && i0 < data.length) {
				i -= 3;
				i0 -= 3;
				i1 -= 3;
				i2 -= 3;
				imv_img1.setImageBitmap(data[i0]);
				imv_img2.setImageBitmap(data[i1]);
				imv_img3.setImageBitmap(data[i2]);
				imv_img1.setEnabled(true);
				imv_img2.setEnabled(true);
				imv_img3.setEnabled(true);
				tv_txt1.setText(name_service.get(i0));
				tv_txt2.setText(name_service.get(i1));
				tv_txt3.setText(name_service.get(i2));
			}
			break;
		case R.id.image1:
			switch (i0) {
			case 0:
				((Global) getApplicationContext()).nameShop = (name_service
						.get(i0));
				((Global) getApplicationContext()).Servicesname = "food_list";
				((Global) getApplicationContext()).type_cart = "food";
				startActivity(new Intent(Service.this, Services_list.class));
				break;
			case 3:
				((Global) getApplicationContext()).nameShop = (name_service
						.get(i0));
				((Global) getApplicationContext()).Servicesname = "laundry_list";
				((Global) getApplicationContext()).type_cart = "laundry";
				startActivity(new Intent(Service.this, Services_list.class));
				break;
			}
			break;
		case R.id.image2:
			switch (i1) {
			case 1:
				((Global) getApplicationContext()).nameShop = (name_service
						.get(i1));
				((Global) getApplicationContext()).Servicesname = "massage_list";
				((Global) getApplicationContext()).type_cart = "massage";
				startActivity(new Intent(Service.this, Services_list.class));
				break;
			}
			break;
		case R.id.image3:
			switch (i2) {
			case 2:
				((Global) getApplicationContext()).nameShop = (name_service
						.get(i2));
				((Global) getApplicationContext()).Servicesname = "car_list";
				((Global) getApplicationContext()).type_cart = "carent";
				startActivity(new Intent(Service.this, Services_list.class));
				break;
			}
			break;
		case R.id.imagehome:
			startActivity(new Intent(Service.this, home.class));
			break;		
		case R.id.image_cart:
			startActivity(new Intent(Service.this, Add_cart.class));
			break;
		case R.id.image_msg:
			startActivity(new Intent(Service.this, Message_list.class));
			break;
		case R.id.image_bill:
			startActivity(new Intent(Service.this, Billing_history.class));
			break;
		}
	}

	private Bitmap getBitmapFromURL(String tmp) {
		// TODO Auto-generated method stub
		try {
			URL url = new URL(tmp);
			InputStream input = url.openConnection().getInputStream();
			BitmapFactory.Options options = new BitmapFactory.Options();
			Bitmap myBitmap = BitmapFactory.decodeStream(input, null, options);
			return myBitmap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void getService_cat() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "service_cat";
		String NAMESPACE = "";
		String METHOD_NAME = "service_cat";
		String imei = ((Global) getApplicationContext()).imei;
		String lang = ((Global) getApplicationContext()).lang;
		// String imei = "1";
		String str = "";
		String message = "";
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		// request.addProperty("room","0001");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			String[] a = b.split("\\|");
			indexSub = a[a.length - 1].indexOf("}");
			b = a[a.length - 1].substring(0, indexSub - 2);
			a[a.length - 1] = b;
			for (int i = 0; i < a.length; i++) {
				String[] tmp = a[i].split("\\*");
				name_service.add(tmp[0]);
				image_service.add(tmp[1]);

			}

		} catch (Exception e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}

	}

}
